//
//  APHomeView.m
//  AqualPolo
//
//  Created by dong on 2024/5/3.
//

#import "APHomeView.h"
@interface APHomeView ()
@property (nonatomic, strong) UIView *topTipView;
@end

@implementation APHomeView



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _topTipView = [[UIView alloc] init];
        _topTipView.layer.borderWidth = 1;
        _topTipView.layer.borderColor = LYRGBColor(72, 67, 193).CGColor;
        _topTipView.backgroundColor = LYRGBColor(10, 24, 69);
        _topTipView.layer.cornerRadius = 8;
        [self addSubview:_topTipView];
        [_topTipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(20);
            make.left.equalTo(self).offset(20);
            make.right.equalTo(self).offset(-20);
            make.height.mas_equalTo(43);
        }];
         UILabel *topTipLabel = [[UILabel alloc] init];
        topTipLabel.textColor = LYRGBColor(79, 68, 209);
        topTipLabel.font = [LYFont fontWithSize:18 fontName:LYFontNamepfb];
        topTipLabel.textAlignment = NSTextAlignmentCenter;
        topTipLabel.text = @"Choose a water volleyballclick to find out.";
        topTipLabel.adjustsFontSizeToFitWidth = true;
        [_topTipView addSubview:topTipLabel];
        [topTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_topTipView);
            make.right.equalTo(_topTipView);
            make.top.bottom.equalTo(_topTipView);
        }];
        
        UIScrollView *scrollView = [[UIScrollView alloc] init];
        [self addSubview:scrollView];
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_topTipView.mas_bottom).offset(20);
            make.left.right.bottom.equalTo(self);
        }];
        
        UIView *contentView = [[UIView alloc] init];
        [scrollView addSubview:contentView];
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(scrollView);
            make.width.equalTo(self);
        }];
        
        _volleyballView = [[APHomeVolleyballView alloc] init];
        UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
        [_volleyballView.contentView addGestureRecognizer:rotationGestureRecognizer];
        [contentView addSubview:_volleyballView];
        [_volleyballView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(contentView);
            make.left.equalTo(contentView).offset(15);
            make.right.equalTo(contentView).offset(-15);
            make.height.equalTo(_volleyballView.mas_width).multipliedBy(336.0 / 348.0);
        }];
        
        _bubbleView = [[APHomeUnlockBubbleView alloc] init];
        _bubbleView.ballType = APBallTypeNoviceDancer;
        [contentView addSubview:_bubbleView];
        [_bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_volleyballView.mas_bottom).offset(-7);
            make.right.equalTo(contentView).offset(-90);
            make.width.mas_equalTo(125);
        }];
        
        _clickButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _clickButton.backgroundColor = LYRGBColor(87, 61, 255);
        _clickButton.layer.cornerRadius = 8;
        _clickButton.layer.masksToBounds = true;
        _clickButton.layer.shadowColor = LYRGBColor(68, 45, 220).CGColor;
        _clickButton.layer.shadowOffset = CGSizeMake(2, 2);
        _clickButton.layer.shadowRadius = 8;
        _clickButton.layer.shadowOpacity = 1;
        _clickButton.titleLabel.font = [LYFont fontWithSize:18 fontName:LYFontNamepfb];
        [_clickButton setTitle:@"To unlock" forState:UIControlStateNormal];
        [contentView addSubview:_clickButton];
        [_clickButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contentView).offset(90);
            make.right.equalTo(contentView).offset(-90);
            make.top.equalTo(_bubbleView.mas_bottom);
            make.height.mas_equalTo(40);
        }];
        
        _userView = [[APHomeUserView alloc] init];
        [contentView addSubview:_userView];
        [_userView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_clickButton.mas_bottom).offset(16);
            make.left.equalTo(contentView).offset(18);
            make.right.equalTo(contentView).offset(-18);
            make.height.mas_equalTo(235);
            make.bottom.equalTo(contentView);
        }];
        
    }
    return self;
}

- (void)rotateView:(UIRotationGestureRecognizer *)gesture{
    LYLog(@"=========%@",gesture);
    UIView *view = gesture.view;
       if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
           view.transform = CGAffineTransformRotate(view.transform, gesture.rotation);
           [gesture setRotation:0];
       }
}


- (void)safeAreaInsetsDidChange{
    [super safeAreaInsetsDidChange];
    [_topTipView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(self.safeAreaInsets.top);
    }];
}

@end


@implementation APHomeVolleyballView

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
    }
    return _contentView;
}

- (void)startRotation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//rotation.z
     animation.toValue =   [NSNumber numberWithFloat: M_PI *2];
     animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
     animation.duration = 60;
     animation.autoreverses = NO;
     animation.cumulative = NO;
     animation.removedOnCompletion = NO;
     animation.fillMode = kCAFillModeForwards;
     animation.repeatCount = FLT_MAX;
     [self.contentView.layer addAnimation:animation forKey:@"rotation"];
}

- (void)stopRotation{
    [self.contentView.layer removeAnimationForKey:@"rotation"];
}
//1.Novice Dancer 2.Intermediate Warrior 3.Senior Dominator 4.Elite Leader 5.Legend King 6.Supreme God
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UIImageView *backgroundImageView = [[UIImageView alloc] init];
        backgroundImageView.image = [UIImage imageNamed:@"home_circle_bg"];
        [self addSubview:backgroundImageView];
        [backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self addSubview:self.contentView];
//        [self startRotation];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    
    
        
        _noviceDancerItem = [[APHomeBallItem alloc] init];
        _noviceDancerItem.ballType = APBallTypeNoviceDancer;
        [self.contentView addSubview:_noviceDancerItem];
        [_noviceDancerItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(18);
            make.centerX.equalTo(self.contentView).offset(70);
        }];
        
        _intermediateWarriorItem = [[APHomeBallItem alloc] init];
        _intermediateWarriorItem.ballType = APBallTypeIntermediateWarrior;
        [self.contentView addSubview:_intermediateWarriorItem];
        [_intermediateWarriorItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_noviceDancerItem.mas_top).offset(26);
            make.left.equalTo(self.contentView).offset(65);
        }];
        
        _seniorDominatorItem = [[APHomeBallItem alloc] init];
        _seniorDominatorItem.ballType = APBallTypeSeniorDominator;
        [self.contentView addSubview:_seniorDominatorItem];
        [_seniorDominatorItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView);
            make.left.equalTo(self.contentView).offset(37);
        }];
        
        _eliteLeaderItem = [[APHomeBallItem alloc] init];
        _eliteLeaderItem.ballType = APBallTypeLeader;
        [self.contentView addSubview:_eliteLeaderItem];
        [_eliteLeaderItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-16);
            make.left.equalTo(self.contentView).offset(74);
        }];
        
        
        _legendKingItem = [[APHomeBallItem alloc] init];
        _legendKingItem.ballType = APBallTypeLegendKing;
        [self.contentView addSubview:_legendKingItem];
        [_legendKingItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView).offset(-34);
            make.right.equalTo(self.contentView).offset(-51);
        }];
        
        _supremeGodItem = [[APHomeBallItem alloc] init];
        _supremeGodItem.ballType = APBallTypeSupremeGod;
        [self.contentView addSubview:_supremeGodItem];
        [_supremeGodItem mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.contentView.mas_centerY).offset(11);
            make.right.equalTo(self.contentView);
        }];
        
        _selectedBallView = [[APHomeSelectedBallView alloc] init];
        _selectedBallView.ballType = APBallTypeNoviceDancer;
        [self addSubview:_selectedBallView];
        [_selectedBallView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).offset(-50);
        }];
        
        
    }
    return self;
}

@end

@implementation APHomeSelectedBallView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageView = [[UIImageView alloc] init];
        [self addSubview:_imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(74, 74));
        }];
        
        _coverView = [[UIView alloc] init];
        _coverView.backgroundColor = LYRGBAColor(0, 0, 0, 0.4);
        _coverView.layer.cornerRadius = 37;
        _coverView.layer.masksToBounds = true;
        [_imageView addSubview:_coverView];
        [_coverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_imageView);
        }];
        
        _lockImageView = [[UIImageView alloc] init];
        _lockImageView.image = [UIImage imageNamed:@"home_lock"];
        [_coverView addSubview:_lockImageView];
        [_lockImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_coverView);
        }];
        
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = LYRGBColor(154, 255, 213);
        _nameLabel.font = [LYFont fontWithSize:12 fontName:LYFontNamepfr];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        [_nameLabel setGradientBackgroundWithColors:@[LYRGBColor(19, 78, 104),LYRGBColor(25, 15, 85)] locations:@[@0,@1] startPoint:CGPointMake(0.5, 0) endPoint:CGPointMake(0.5, 1)];
        _nameLabel.layer.borderColor = LYRGBColor(45, 116, 255).CGColor;
        _nameLabel.layer.borderWidth = 0.5;
        _nameLabel.layer.cornerRadius = 4;
        _nameLabel.layer.masksToBounds = true;
        [self addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_imageView.mas_bottom).offset(10);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(22);
        }];
    }
    return self;
}

- (void)setBallType:(APBallType)ballType{
    _ballType = ballType;
    _imageView.image = [UIImage imageNamed:[BaseDataManager ballImageName:ballType]];
    _nameLabel.text = [NSString stringWithFormat:@"【%@】",[BaseDataManager ballName:ballType]];
}

@end
